﻿using BlockGame.Components;
using BlockGame.Game;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Threading;

namespace BlockGame
{
    class Program
    {
        public static Settings Settings = new Settings();

        static void Main(string[] args)
        {
            /**
             * Initialisiere die Umgebung
             */
            Init();

            /**
             * Zeige das Intro
             */
            ShowIntro();

            /**
             * Zeige das Hauptmenü
             */
            ShowMainMenu();
        }

        public static void Init()
        {
            Console.SetBufferSize((Int32)((Dictionary<String, Object>)Settings.SettingsList["Window"])["Width"], (Int32)((Dictionary<String, Object>)Settings.SettingsList["Window"])["Height"]);
            Console.WindowHeight = (Int32)((Dictionary<String, Object>)Settings.SettingsList["Window"])["Height"];
            Console.WindowWidth = (Int32)((Dictionary<String, Object>)Settings.SettingsList["Window"])["Width"];
            Console.ForegroundColor = (ConsoleColor)((Dictionary<String, Object>)Settings.SettingsList["Window"])["ForegroundNormal"];
            Console.BackgroundColor = (ConsoleColor)((Dictionary<String, Object>)Settings.SettingsList["Window"])["BackgroundNormal"];
            Console.CursorVisible = (Boolean)((Dictionary<String, Object>)Settings.SettingsList["Window"])["CursorVisible"];

            /**
             * Prüfe die Spielumgebung
             */
            // Ordner
            if (!Directory.Exists(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + (String)((Dictionary<String, Object>)Settings.SettingsList["Game"])["SavePath"]))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + (String)((Dictionary<String, Object>)Settings.SettingsList["Game"])["SavePath"]);
            }
        }

        public static void ShowIntro()
        {
            for (Int32 i = 4; i > 0; i--)
            {
                switch(i)
                {
                    case 4:
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                        } break;
                    case 3:
                        {
                            Console.ForegroundColor = ConsoleColor.Yellow;
                        } break;
                    case 2:
                        {
                            Console.ForegroundColor = ConsoleColor.Green;
                        } break;
                    case 1:
                        {
                            Console.ForegroundColor = ConsoleColor.White;
                        } break;
                }

                if ((i - 1) == 0)
                {
                    UiWriter.WriteText("Let's Go !", UiWriter.Direction.Center);
                }
                else
                {
                    UiWriter.WriteText((i - 1).ToString(), UiWriter.Direction.Center);
                }
                Console.Beep();
                Thread.Sleep(1000);
            }
        }

        public static void ShowMainMenu()
        {
            Boolean _exit = false;
            Boolean _waiting = true;
            ConsoleKeyInfo _key = new ConsoleKeyInfo();
            Int32 _currentMenuPosition = 1;
            Int32 _cursorPos = 7;
            GameCore _game = new GameCore();

            do
            {
                _waiting = true;
                while (_waiting)
                {
                    _key = new ConsoleKeyInfo();
                    Console.Clear();
                    Console.Title = "BlockGame v" + ((Version)Settings.SettingsList["Version"]).Major + "." + ((Version)Settings.SettingsList["Version"]).Minor + "." + ((Version)Settings.SettingsList["Version"]).Build;

                    // Title
                    UiWriter.WriteText("BlockGame Version " + ((Version)Settings.SettingsList["Version"]).Major + "." + ((Version)Settings.SettingsList["Version"]).Minor + "." + ((Version)Settings.SettingsList["Version"]).Build + " (InDev)", UiWriter.Direction.Center, new Position(2, 0));

                    // Menu-Items
                    UiWriter.WriteText("Spiel starten", UiWriter.Direction.Center, new Position(7, 0));
                    UiWriter.WriteText("Einstellungen", UiWriter.Direction.Center, new Position(8, 0));
                    UiWriter.WriteText("Über dieses Spiel", UiWriter.Direction.Center, new Position(9, 0));
                    UiWriter.WriteText("Besuche die Entwicklerhomepage", UiWriter.Direction.Center, new Position(10, 0));
                    UiWriter.WriteText("Spiel beenden", UiWriter.Direction.Center, new Position(11, 0));

                    // Setze den Cursor an das erste Menü-Item
                    Console.CursorTop = _cursorPos;
                    Console.CursorLeft = 35;
                    Console.Write("=>");

                    _key = Console.ReadKey();
                    _waiting = false;
                }

                switch (_key.Key)
                {
                    case ConsoleKey.Escape:
                        {
                            // Spiel beenden
                            _exit = true;
                        } break;
                    case ConsoleKey.Enter:
                    case ConsoleKey.Spacebar:
                        {
                            // Auswahl ausführen
                            switch(_currentMenuPosition)
                            {
                                case 1:
                                    {
                                        // Spiel starten
                                        _game.Start();
                                    } break;
                                case 2:
                                    {
                                        // Einstellungen
                                        ShowSettingsScreen();
                                    } break;
                                case 3:
                                    {
                                        // Über dieses Spiel
                                        ShowAboutScreen();
                                    } break;
                                case 4:
                                    {
                                        // Besuche die Entwickler-Homepage
                                        Process.Start("http://www.lkraemer.de");
                                    } break;
                                case 5:
                                    {
                                        // Spiel beenden
                                        _exit = true;
                                    } break;
                            }
                        } break;
                    case ConsoleKey.UpArrow:
                        {
                            // Einen Eintrag nach oben
                            if (_currentMenuPosition > 1)
                            {
                                _cursorPos--;
                                _currentMenuPosition--;
                            }
                        } break;
                    case ConsoleKey.DownArrow:
                        {
                            // Einen Eintrag nach unten
                            if (_currentMenuPosition < 5)
                            {
                                _cursorPos++;
                                _currentMenuPosition++;
                            }
                        } break;
                    default:
                        {
                            // Anzeige bei ungültiger Eingabe
                            //ShowInvalidKeyMessage();
                        } break;
                }
            } while (!_exit);

            ShowExitScreen();
        }

        public static void ShowInvalidKeyMessage()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Red;
            UiWriter.WriteText("Sie haben eine ungültige Taste gedrückt.", UiWriter.Direction.Center, new Position(7, 0));
            Console.ForegroundColor = (ConsoleColor)((Dictionary<String, Object>)Settings.SettingsList["Window"])["ForegroundNormal"];

            Boolean _exit = false;
            do
            {
                Console.ReadKey();
                _exit = true;
            } while (!_exit);

            return;
        }

        public static void ShowSettingsScreen()
        {
            Console.Clear();
            UiWriter.WriteText("BlockGame Einstellungen", UiWriter.Direction.Center, new Position(2, 0));

            Boolean _exit = false;
            do
            {
                Console.ReadKey();
                _exit = true;
            } while (!_exit);

            return;
        }

        public static void ShowAboutScreen()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.Gray;
            UiWriter.WriteText("Über BlockGame", UiWriter.Direction.Center, new Position(7, 0));
            UiWriter.WriteText("v" + ((Version)Settings.SettingsList["Version"]).Major + "." + ((Version)Settings.SettingsList["Version"]).Minor + "." + ((Version)Settings.SettingsList["Version"]).Build + "", UiWriter.Direction.Center, new Position(8, 0));

            Console.ForegroundColor = ConsoleColor.Green;
            UiWriter.WriteText("Development by Xe77a", UiWriter.Direction.Center, new Position(13, 0));
            UiWriter.WriteText("Publishing by lkraemer.de", UiWriter.Direction.Center, new Position(14, 0));
            UiWriter.WriteText("Copyright 2013 lkraemer.de", UiWriter.Direction.Center, new Position(15, 0));

            Console.ForegroundColor = ConsoleColor.Cyan;
            UiWriter.WriteText("Written in C# and .NET", UiWriter.Direction.Center, new Position(20, 0));
            UiWriter.WriteText("Available at http://gameblock.codeplex.com", UiWriter.Direction.Center, new Position(21, 0));

            Console.ForegroundColor = (ConsoleColor)((Dictionary<String, Object>)Settings.SettingsList["Window"])["ForegroundNormal"];

            Boolean _exit = false;

            do
            {
                Console.ReadKey();
                _exit = true;
            } while (!_exit);

            return;
        }

        public static void ShowExitScreen()
        {
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.DarkGray;
            UiWriter.WriteText("Das Spiel wird beendet", UiWriter.Direction.Center, new Position(7, 0));

            Console.ForegroundColor = ConsoleColor.Cyan;
            UiWriter.WriteText("Vielen Dank", UiWriter.Direction.Center, new Position(10, 0));

            Console.ForegroundColor = ConsoleColor.Green;
            UiWriter.WriteText("Development by Xe77a", UiWriter.Direction.Center, new Position(13, 0));
            UiWriter.WriteText("Publishing by lkraemer.de", UiWriter.Direction.Center, new Position(14, 0));
            UiWriter.WriteText("Copyright 2013 lkraemer.de", UiWriter.Direction.Center, new Position(15, 0));

            Console.ForegroundColor = (ConsoleColor)((Dictionary<String, Object>)Settings.SettingsList["Window"])["ForegroundNormal"];
            Console.SetCursorPosition(2, (Console.WindowHeight - 2));
        }
    }
}
